package org.activequant.dao.hibernate.types;

import org.activequant.core.types.TimeFrame;

/**
 * Hibernate helper to convert TimeFrame to/from SQL type.
 * <br>
 * TimeFrameType extends SimpleType. Holds the following inherited variable:
 * <ul>
 * <li>sqlTypes(int[])</li>
 * </ul>
 * <b>History:</b><br>
 *  - [02.12.2007] Created (Mike Kroutikov)<br>
 *
 *  @author Mike Kroutikov
 */
public class TimeFrameType extends SimpleType {
	/**
	 * returns <code>TimeFrame.class</code>
	 */
	public Class<TimeFrame> returnedClass() {
		return TimeFrame.class;
	}
	/**
	 * Returns a String representation of the given obj(TimeFrame) in the form "${length}${unit.acronym}" using its length(int) and unit(Unit) enum.<br/> 
	 * For example "2h" (2 hours)
	 */
	@Override
	protected String format(Object obj) {
		return ((TimeFrame) obj).toString();
	}
	/**
	 * Creates and returns a TimeFrame by parsing the given val(String).<br/>
	 * The given val(String) has to be in the form "${length}${acronym}", where length is a number and acronym is <strong>"s"</strong>, <strong>"m"</strong>, <strong>"h"</strong>, <strong>"d"</strong>, <strong>"w"</strong>, <strong>"M"</strong> or <strong>"Y"</strong><br/>
	 * The returned TimeFrame will have that length(int) and unit(Unit) enum matching that acronym.
	 */
	@Override
	protected Object parse(String val) {
		return TimeFrame.parse(val);
	}
}
